
/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * 
 * @author Philippe Bordron
 *
 */

package combi.sipper.io;

import java.io.*;
import java.util.*;

public class IOList {

	private String fileName;
	private static String SEPARATOR = "\n";
	
	public IOList(String fileName)
	{
		setFile(fileName);
	}
	
	public List<String> read() throws IOException
	{
		List<String> result = new ArrayList<String>();
		BufferedReader in = new BufferedReader(new FileReader(fileName));	
		String ch;
		while ((ch = in.readLine()) != null)
		{
			result.addAll(Arrays.asList(ch.split("\\s+")));
		}
		in.close();
		return result;
	}

	public void write(Collection<String> col) throws IOException
	{
		write(col, false);
	}
	
	public void write(Collection<String> col, boolean append) throws IOException
	{
		BufferedWriter out = new BufferedWriter(new FileWriter(fileName, append));	
		for (String elem : col)
		{
			out.write(elem.toString()+SEPARATOR);
		}
		out.close();
	}
	
	public void setFile(String fileName)
	{
		this.fileName = fileName;
	}
	
	public String getFileName()
	{
		return this.fileName;
	}
	
	
}
